package com.xac.common.util.io;

import java.io.ByteArrayOutputStream;

import android.app.Activity;
import android.content.Context;
import android.content.Intent;
import android.graphics.Bitmap;
import android.graphics.Bitmap.Config;
import android.graphics.BitmapFactory;
import android.graphics.BitmapShader;
import android.graphics.BlurMaskFilter;
import android.graphics.Canvas;
import android.graphics.Matrix;
import android.graphics.Paint;
import android.graphics.PixelFormat;
import android.graphics.PorterDuff.Mode;
import android.graphics.PorterDuffXfermode;
import android.graphics.Rect;
import android.graphics.RectF;
import android.graphics.Shader;
import android.graphics.drawable.Drawable;
import android.graphics.drawable.ShapeDrawable;
import android.graphics.drawable.shapes.RoundRectShape;
import android.graphics.drawable.shapes.Shape;
import android.net.Uri;
import android.widget.ImageView;

/**
 * @author frank
 * 
 */
public class ImageUtil {

	public static byte[] Bitmap2Bytes(Bitmap bm) {
		ByteArrayOutputStream baos = new ByteArrayOutputStream();
		bm.compress(Bitmap.CompressFormat.PNG, 100, baos);
		return baos.toByteArray();
	}

	public static Bitmap Bytes2Bimap(byte[] b) {
		if (b.length != 0) {
			return BitmapFactory.decodeByteArray(b, 0, b.length);
		} else {
			return null;
		}
	}

	/**
	 * 将图片变成圆角
	 * 
	 * @param bitmap 要处理的图片
	 * @return 转换完毕的图片
	 */
	public static Bitmap getRoundedCornerBitmap(Bitmap bitmap) {
		Bitmap output = Bitmap.createBitmap(bitmap.getWidth(), bitmap.getHeight(), Config.ARGB_8888);
		Canvas canvas = new Canvas(output);

		final int color = 0xff424242;
		final Paint paint = new Paint();
		final Rect rect = new Rect(0, 0, bitmap.getWidth(), bitmap.getHeight());
		final RectF rectF = new RectF(rect);
		final float roundPx = 8;

		paint.setAntiAlias(true);
		canvas.drawARGB(0, 0, 0, 0);
		paint.setColor(color);
		canvas.drawRoundRect(rectF, roundPx, roundPx, paint);

		paint.setXfermode(new PorterDuffXfermode(Mode.SRC_IN));
		canvas.drawBitmap(bitmap, rect, rect, paint);
		return output;
	}

	/**
	 * Drawable转换成Bitmap
	 * 
	 * @param drawable
	 * @return Bitmap
	 */
	public static Bitmap drawableToBitmap(Drawable drawable) {

		int width = drawable.getIntrinsicWidth();
		int height = drawable.getIntrinsicHeight();
		Bitmap bitmap = Bitmap.createBitmap(width, height, 
				drawable.getOpacity() != PixelFormat.OPAQUE ? Bitmap.Config.ARGB_8888 : Bitmap.Config.RGB_565);
		drawable.setBounds(0, 0, width, height);
		drawable.draw(new Canvas(bitmap));
		return bitmap;
	}

	public static Bitmap PicZoom(Bitmap bmp, int width, int height) {
		if (bmp == null) {
			return null;
		}
		int bmpWidth = bmp.getWidth();
		int bmpHeight = bmp.getHeight();
		Matrix matrix = new Matrix();
		matrix.postScale((float) width / bmpWidth, (float) height / bmpHeight);

		Bitmap bitmap = Bitmap.createBitmap(bmp, 0, 0, bmpWidth, bmpHeight, matrix, true);
		if (!bmp.isRecycled()) {
			bmp.isRecycled();
		}
		return bitmap;
	}

	/**
	 * 头像裁剪
	 * 
	 * @param uri
	 * @param requestCode
	 * @param context
	 */
	public static void startPhotoZoom(Uri uri, int requestCode, Activity context) {
		Intent intent = new Intent("com.android.camera.action.CROP");
		intent.setDataAndType(uri, "image/*");
		intent.putExtra("crop", "true");
		// aspectX aspectY 是宽高的比例
		intent.putExtra("aspectX", 1);
		intent.putExtra("aspectY", 1);
		// outputX outputY 是裁剪图片宽高
		intent.putExtra("outputX", 64);
		intent.putExtra("outputY", 64);
		intent.putExtra("return-data", true);
		context.startActivityForResult(intent, requestCode);
	}

	/**
	 * 用于给指定头像生成圆角的头像
	 * 
	 * @param bm Bitmap
	 * @param width 生成图片的宽度
	 * @param height 生成图片的高度
	 * @param adii 生成圆角的值
	 * @return Drawbale
	 */
	public static void decorateImageView(Bitmap bitmap, int width, int height, int adii, ImageView imageView) {
		Shape shpRound = new RoundRectShape(new float[] { adii, adii, adii, adii, adii, adii, adii, adii }, null, null);
		ShapeDrawable dwbRound = new ShapeDrawable(shpRound);
		dwbRound.setIntrinsicWidth(width);
		dwbRound.setIntrinsicHeight(height);
		Shader shdBitmap = new BitmapShader(bitmap, Shader.TileMode.MIRROR, Shader.TileMode.MIRROR);
		Matrix matrix = new Matrix();
		matrix.setScale((float) width / bitmap.getWidth(), (float) height / bitmap.getHeight());
		shdBitmap.setLocalMatrix(matrix);
		dwbRound.getPaint().setShader(shdBitmap);
		dwbRound.getPaint().setFlags(dwbRound.getPaint().getFlags() | Paint.ANTI_ALIAS_FLAG);
		imageView.setImageDrawable(dwbRound);
		// 黄广府添加 2011-11-15
		bitmap = null;
	}

	/**
	 * 给ImageView视图添加阴影效果
	 * 
	 * @param imageView ImageView组件
	 * @param drawableId 图片资源的Id
	 * @param context
	 */
	public void drawImageDropShadow(Context context, ImageView iamgeView, int drawableId) {
		BlurMaskFilter blurFilter = new BlurMaskFilter(5, BlurMaskFilter.Blur.SOLID);
		Paint shadowPaint = new Paint();
		shadowPaint.setMaskFilter(blurFilter);

		int[] offsetXY = new int[2];
		Bitmap originalBitmap = BitmapFactory.decodeResource(context.getResources(), drawableId);
		Bitmap shadowBitmap = originalBitmap.extractAlpha(shadowPaint, offsetXY);
		Bitmap shadowImage32 = shadowBitmap.copy(Bitmap.Config.ARGB_8888, true);
		Canvas c = new Canvas(shadowImage32);
		c.drawBitmap(originalBitmap, 0, 0, null);
		iamgeView.setImageBitmap(shadowImage32);
	}

}
